package com.xinqizhi.controller;

import com.xinqizhi.entity.SysRole;
import com.xinqizhi.service.SysRoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统角色管理控制器
 */
@Controller
@RequestMapping("/sysRole")
public class SysRolesController {
    private final static Logger log = Logger.getLogger(SysRolesController.class);

    @Autowired
    private SysRoleService roleService;


    /**
     * 跳转到角色管理主页
     * @return 视图路径以及名称
     */
    @RequestMapping("/roleindex")
    public String toRolesIndex(){
        return "redirect:../views/sysRole.jsp";
    }

    /**
     * 后台超级管理员获取所有角色信息管理
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map getRoleList(){
        Map<String, Object> result = new HashMap<>();
        List<SysRole> roleList = roleService.getRoleData();
        result.put("data",roleList);
        result.put("success",true);
        result.put("code",0);
        result.put("msg","查询所有角色成功");
        return result;
    }

    /**
     * 角色列表的行双击事件，通过role_id查询数据，查看角色详情信息
     * @return
     */
    @RequestMapping("/data")
    @ResponseBody
    public Map getRoleByRoleid(Integer roleId){
        Map<String, Object> result = new HashMap<String, Object>();
        List<SysRole> dataByRoleid = roleService.getRoleDataByRoleid(roleId);
        result.put("data",dataByRoleid.get(0));
        result.put("success",true);
        result.put("code",0);
        result.put("msg","查询所有菜单成功");
        return result;
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequestMapping("/updataRole")
    @ResponseBody
    public Map updataRole(SysRole role){
        Map<String, Object> result = new HashMap<>();
        log.info("菜单修改------------------"+role.toString());
        Boolean uprole = roleService.updataRole(role);
        result.put("success",uprole);
        result.put("code",0);
        result.put("msg","修改菜单请求成功");
        return result;
    }

    /**
     * 跳转到对角色分配菜单主页
     * @return
     */
    @RequestMapping("/role_menu")
    public String toRoleMenuJsp(){
        return "redirect:../views/role-menu.jsp";
    }




}
